package com.tadpole.music.presenter.question;

import com.tadpole.music.bean.question.RealScoreBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.question.RealScoreIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 获取实战真题分数
 * Created By 孙岩
 * on 2020/9/18
 */
public class RealScorePresenter extends BasePresenter<RealScoreIView> {
    /**
     * @param college_id   学院id
     * @param rank_id      等级id
     * @param result       根据题目顺序上传答题结果， 对为true，错位false， 中间用英文逗号隔开 true, false,true,
     * @param err_selected 错误选择的答案，中间用英文逗号隔开
     * @param err_qid      错题id，中间用逗号隔开
     * @param time         答题时间
     */
    @SuppressWarnings("unchecked")
    public void getRealScore(String college_id, String rank_id, String result, String err_selected, String err_qid, final String time) {
        if (isViewAttached()) {
            String url = UrlConfig.getRealScore;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.POST_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .paramKey("college_id", "rank_id", "result", "err_selected", "err_qid", "time")
                    .paramValue(college_id, rank_id, result, err_selected, err_qid, time)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                        RealScoreBean.DataBean bean = new RealScoreBean.DataBean(
                                                JsonParseUtil.getInt(object, "chang"),
                                                JsonParseUtil.getInt(object, "mo"),
                                                JsonParseUtil.getDouble(object, "score"),
                                                JsonParseUtil.getInt(object, "shi"),
                                                JsonParseUtil.getInt(object, "yue"),
                                                JsonParseUtil.getInt(object, "base"),
                                                JsonParseUtil.getInt(object, "ying")
                                        );
                                        getView().showRealScore(bean,time);
                                } else if (obj.getInt("code") == 401) {
                                    getView().show401();
                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {
                            getView().hideLoading();
                        }

                        @Override
                        public void onBefore() {
                            getView().showLoading();
                        }
                    });
        }
    }
}
